<template>
  <div>
     <Row>
        <label>开始时间:</label>
        <Date-picker v-model="startDay" placeholder="开始日期" format="yyyy-MM-dd"></Date-picker>
        <label>结束时间:</label>
        <Date-picker v-model="endDay" placeholder="结束日期" format="yyyy-MM-dd"></Date-picker>
        <label>限制等级:</label>
        <input v-model="level" placeholder="等级" ></input>
        <label>限制创建时间:</label>
        <input v-model="cDay" placeholder="创建时间" ></input>
        <br/>
        <br/>
        <Button type="primary" @click="onRefresh">刷新玩家统计</Button>
        <Button type="primary" @click="onExportBaseoperationData">导出基础运营数据</Button>
        <Button type="primary" @click="onExportMachineBaseData">导出机器基础数据</Button>
        <Button type="primary" @click="onExportBaseGameData(0)">导出免费游戏数据</Button>
        <Button type="primary" @click="onExportBaseGameData(1)">导出小R游戏数据</Button>
        <Button type="primary" @click="onExportBaseGameData(2)">导出中R游戏数据</Button>
        <Button type="primary" @click="onExportBaseGameData(3)">导出大R游戏数据</Button>
        <br/>
        <br/>
        <label>显示功能:</label>
        <Button type="primary" @click="onExportBaseGameDataV2(0)">导出免费数据</Button>
        <Button type="primary" @click="onExportBaseGameDataV2(1)">导出小R数据</Button>
        <Button type="primary" @click="onExportBaseGameDataV2(2)">导出中R数据</Button>
        <Button type="primary" @click="onExportBaseGameDataV2(3)">导出大R数据</Button>
    </Row>
  </div>
</template>
<script>

import {refreshDayGamePlayerData, getMachineData, getBaseOperationData, getBaseFreeGameData, getBasePayGameData, getBaseGameData} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      startDay: '',
      endDay: '',
      tableName: '',
      // gameId: 2,
      count: 0,
      tmpTableData: [],
      liushui: 0,
      shoushu: 0,
      wins: 0,
      level: 999,
      cDay: 20180101,
      tableList: [
        {
          value: 2,
          label: '万圣节'
        },
        {
          value: 3,
          label: '狂野非洲'
        },
        {
          value: 4,
          label: '赏金猎人'
        },
        {
          value: 5,
          label: '图腾'
        },
        {
          value: 6,
          label: '龙战'
        },
        {
          value: 7,
          label: '四神兽'
        },
        {
          value: 8,
          label: '杰克和魔豆'
        },
        {
          value: 9,
          label: '小狗'
        },
        {
          value: 10,
          label: '中国福'
        },
        {
          value: 11,
          label: '图腾2'
        },
        {
          value: 13,
          label: '狂野777'
        },
        {
          value: 14,
          label: 'cityofqueens'
        },
        {
          value: 15,
          label: 'hotter777'
        },
        {
          value: 16,
          label: 'diamond777'
        },
        {
          value: 17,
          label: 'zeus'
        },
        {
          value: 18,
          label: 'doubleGold777'
        },
        {
          value: 19,
          label: 'fuCai'
        }
      ]
    }
  },
  created () {
    this.endDay = new Date()
    this.startDay = new Date().setDate(new Date().getDate() - 1)
    // this.onSearch()
  },
  methods: {
    onRefresh () {
      let sSendDay = 0
      let eSendDay = 0
      let cSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }

      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      cSendDay = parseInt(this.cDay)

      let lev = parseInt(this.level)

      console.log('sSendDay:', sSendDay, ', eSendDay:', eSendDay, ', lev:', lev, ', cSendDay:', cSendDay)
      refreshDayGamePlayerData(sSendDay, eSendDay, lev, cSendDay).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('刷新成功!!!')
        } else {
          this.$Message.error('刷新失败!!!')
        }
      })
    },
    onExportMachineBaseData () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      this.count = 0
      this.tmpTableData = []
      let gameId = this.tableList[this.count].value
      this.onMachineData(sSendDay, gameId, eSendDay)
    },
    onMachineData (sSendDay, gameId, eSendDay) {
      getMachineData(sSendDay, gameId, eSendDay).then((data) => {
        if (!data) {
          return
        }

        let MachineBaseDataVO = getVO('MachineBaseDataVO')
        let pv = data.data.data
        let len = pv.data.length
        console.log('pv.data.length:', pv.data.length)
        for (let i = 0; i < len; i++) {
          let vo = MachineBaseDataVO.decode(pv.data[i])
          vo.totalShouShu = vo.totalShouShu.toNumber()
          vo.freePlayerNum = vo.freePlayerNum.toNumber()
          vo.xiaoRPlayerNum = vo.xiaoRPlayerNum.toNumber()
          vo.zhongRPlayerNum = vo.zhongRPlayerNum.toNumber()
          vo.daRPlayerNum = vo.daRPlayerNum.toNumber()
          vo.totalLiuShui = vo.totalLiuShui.toNumber()
          vo.totalWins = vo.totalWins.toNumber()
          this.tmpTableData.push(vo)
        }

        let len2 = this.tableList.length
        if (this.count === len2 - 1) {
          this.tableName = 'machine_base_data'
          exportExcel(this.tableName, this.tmpTableData)
          console.log('sSendDay:', sSendDay)
        } else {
          this.count++
          let gameId = this.tableList[this.count].value
          this.onMachineData(sSendDay, gameId, eSendDay)
        }
      })
    },
    onExportBaseoperationData () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }
      let lev = parseInt(this.level)
      getBaseOperationData(sSendDay, eSendDay, lev).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []

        let BaseOperationDataVO = getVO('BaseOperationDataVO')
        let pv = data.data.data
        let len = pv.data.length
        console.log('pv.data.length:', pv.data.length)
        for (let i = 0; i < len; i++) {
          let vo = BaseOperationDataVO.decode(pv.data[i])
          allTableData.push(vo)
        }

        this.tableName = 'base_operation_data'
        exportExcel(this.tableName, allTableData)
        console.log('export end!!!')
      })
    },
    onExportBaseFreeGameData () {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }
      getBaseFreeGameData(sSendDay, eSendDay).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []

        let BaseFreeGameDataVO = getVO('BaseFreeGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BaseFreeGameDataVO.decode(pv.data[i])
          allTableData.push(vo)
        }

        this.tableName = 'base_free_data'
        exportExcel(this.tableName, allTableData)
        console.log('export free game data end!!!')
      })
    },
    onBaseFreeGameData (sSendDay, eSendDay) {
      getBaseFreeGameData(sSendDay, this.liushui, this.shoushu, this.wins, eSendDay).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []

        let BaseFreeGameDataVO = getVO('BaseFreeGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BaseFreeGameDataVO.decode(pv.data[i])
          allTableData.push(vo)
        }

        this.tableName = 'base_free_data'
        exportExcel(this.tableName, allTableData)
        console.log('export free game data end!!!')
      })
    },
    onExportBasePayGameData (type) {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }

      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }
      getBasePayGameData(sSendDay, type, eSendDay).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []

        let BasePayGameDataVO = getVO('BasePayGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BasePayGameDataVO.decode(pv.data[i])
          allTableData.push(vo)
        }

        this.tableName = 'base_pay_data_' + type
        exportExcel(this.tableName, allTableData)
        console.log('export pay game data end!!!')
      })
    },
    onBasePayGameData (sSendDay, type, eSendDay) {
      getBasePayGameData(sSendDay, type, this.liushui, this.shoushu, this.wins, eSendDay).then((data) => {
        if (!data) {
          return
        }
        let allTableData = []
        let BasePayGameDataVO = getVO('BasePayGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BasePayGameDataVO.decode(pv.data[i])
          allTableData.push(vo)
        }

        this.tableName = 'base_pay_data_' + type
        exportExcel(this.tableName, allTableData)
        console.log('export pay game data end!!!')
      })
    },
    onExportBaseGameDataV2 (type) {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }

      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      this.count = 0
      this.tmpTableData = []
      this.liushui = 0
      this.shoushu = 0
      this.wins = 0
      let gameId = this.tableList[this.count].value
      this.onBaseGameDataV2(sSendDay, type, gameId, eSendDay)
    },
    onBaseGameDataV2 (sSendDay, type, gameId, eSendDay) {
      getBaseGameData(sSendDay, type, gameId, eSendDay).then((data) => {
        if (!data) {
          return
        }

        let BaseGameDataVO = getVO('BaseGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BaseGameDataVO.decode(pv.data[i])
          vo.liuShui = vo.liuShui.toNumber()
          vo.shouShu = vo.shouShu.toNumber()
          vo.wins = vo.wins.toNumber()
          console.log('export pay game data end ! vo.liuShui:', vo.liuShui, 'vo.shouShu:', vo.shouShu, ',vo.wins:', vo.wins)
          this.tmpTableData.push(vo)
        }
        let len2 = this.tableList.length
        if (this.count === len2 - 1) {
          this.tableName = 'base_game_data_' + type
          exportExcel(this.tableName, this.tmpTableData)
          console.log('export pay game data end ! this.liuShui:', this.liushui, ',this.shoushu:', this.shoushu, ',this.wins:', this.wins)
        } else {
          this.count++
          let gameId = this.tableList[this.count].value
          this.onBaseGameDataV2(sSendDay, type, gameId, eSendDay)
        }
      })
    },
    onExportBaseGameData (type) {
      let sSendDay = 0
      let eSendDay = 0
      if (this.startDay !== '') {
        let sDay = new Date(this.startDay)
        sSendDay = '' + sDay.getFullYear() + ((sDay.getMonth() + 1) < 10 ? '0' + (sDay.getMonth() + 1) : (sDay.getMonth() + 1)) + (sDay.getDate() < 10 ? '0' + sDay.getDate() : sDay.getDate())
        sSendDay = parseInt(sSendDay)
      }
      if (this.endDay !== '') {
        let eDay = new Date(this.endDay)
        eSendDay = '' + eDay.getFullYear() + ((eDay.getMonth() + 1) < 10 ? '0' + (eDay.getMonth() + 1) : (eDay.getMonth() + 1)) + (eDay.getDate() < 10 ? '0' + eDay.getDate() : eDay.getDate())
        eSendDay = parseInt(eSendDay)
      }

      this.count = 0
      this.tmpTableData = []
      this.liushui = 0
      this.shoushu = 0
      this.wins = 0
      let gameId = this.tableList[this.count].value
      this.onBaseGameData(sSendDay, type, gameId, eSendDay)
    },
    onBaseGameData (sSendDay, type, gameId, eSendDay) {
      getBaseGameData(sSendDay, type, gameId, eSendDay).then((data) => {
        if (!data) {
          return
        }

        let BaseGameDataVO = getVO('BaseGameDataVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = BaseGameDataVO.decode(pv.data[i])
          // vo.liuShui = vo.liuShui.toNumber()
          // vo.shouShu = vo.shouShu.toNumber()
          // vo.wins = vo.wins.toNumber()
          console.log('export pay game data end ! vo.liuShui:', vo.liuShui, 'vo.shouShu:', vo.shouShu, ',vo.wins:', vo.wins)
          if (vo.liuShui > 0) {
            this.liushui = this.liushui + vo.liuShui.toNumber()
          }
          if (vo.shouShu > 0) {
            this.shoushu = this.shoushu + vo.shouShu.toNumber()
          }
          if (vo.wins > 0) {
            this.wins = this.wins + vo.wins.toNumber()
          }
        }
        let len2 = this.tableList.length
        if (this.count === len2 - 1) {
          if (type === 0) {
            this.onBaseFreeGameData(sSendDay, eSendDay)
          } else {
            this.onBasePayGameData(sSendDay, type, eSendDay)
          }
          // let obj = {}
          // obj.day = sSendDay
          // obj.liushui = this.liushui
          // obj.shoushu = this.shoushu
          // obj.wins = this.wins
          // this.tmpTableData.push(obj)
          // this.tableName = 'base_game_data_' + type
          // exportExcel(this.tableName, this.tmpTableData)
          // console.log('export pay game data end ! this.liuShui:', this.liushui, ',this.shoushu:', this.shoushu, ',this.wins:', this.wins)
        } else {
          this.count++
          let gameId = this.tableList[this.count].value
          this.onBaseGameData(sSendDay, type, gameId, eSendDay)
        }
      })
    }
  }
}
</script>
<style scoped>
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}
</style>
